package com.common.controller;

import com.common.model.Result;
import com.common.model.TaskCondition;
import com.common.service.TasksService;
import com.mrobuy.jdvc.entity.Task;
import com.scheduled.TaskSwitch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务运行开关
 */
@RestController
public class TaskEnableController {

	private static Logger LOG = LoggerFactory.getLogger(TaskEnableController.class);

	@RequestMapping("/scheduled/status")
	public Map<String, Boolean> status() {

		Map<String, Boolean> result = new HashMap<>();

		result.put("CATE_SYNC", TaskSwitch.CATE_SYNC.get());
		result.put("PRIMARY_PIC", TaskSwitch.PRIMARY_PIC.get());
		result.put("APPLY_STATE_SYNC", TaskSwitch.APPLY_STATE_SYNC.get());
		result.put("APPLY_SYNC", TaskSwitch.APPLY_SYNC.get());
		result.put("PRODUCT_INFO_SYNC", TaskSwitch.PRODUCT_INFO_SYNC.get());
		result.put("ITEM_IMPORT", TaskSwitch.ITEM_IMPORT.get());
		result.put("ITEM_PUSH", TaskSwitch.ITEM_PUSH.get());
		result.put("ITEM_PREHANDL", TaskSwitch.ITEM_PREHANDL.get());
		result.put("TASK_CHECK", TaskSwitch.TASK_CHECK.get());

		return  result;
	}


	@RequestMapping("/scheduled/enable")
	public Result enable(@RequestParam String name) {

		switch (name) {
			case "CATE_SYNC":
				TaskSwitch.CATE_SYNC.set(true);
				break;
			case "PRIMARY_PIC":
				TaskSwitch.PRIMARY_PIC.set(true);
				break;
			case "APPLY_STATE_SYNC":
				TaskSwitch.APPLY_STATE_SYNC.set(true);
				break;
			case "APPLY_SYNC":
				TaskSwitch.APPLY_SYNC.set(true);
				break;
			case "PRODUCT_INFO_SYNC":
				TaskSwitch.PRODUCT_INFO_SYNC.set(true);
				break;
			case "ITEM_IMPORT":
				TaskSwitch.ITEM_IMPORT.set(true);
				break;
			case "ITEM_PUSH":
				TaskSwitch.ITEM_PUSH.set(true);
				break;
			case "ITEM_PREHANDL":
				TaskSwitch.ITEM_PREHANDL.set(true);
				break;
			case "TASK_CHECK":
				TaskSwitch.TASK_CHECK.set(true);
				break;
				default:
					return new Result(-1, 0, "Name: " + name + " 未知.");
		}

		return  Result.EMPTY;
	}

	@RequestMapping("/scheduled/disable")
	public Result disable(@RequestParam String name) {

		switch (name) {
			case "CATE_SYNC":
				TaskSwitch.CATE_SYNC.set(false);
				break;
			case "PRIMARY_PIC":
				TaskSwitch.PRIMARY_PIC.set(false);
				break;
			case "APPLY_STATE_SYNC":
				TaskSwitch.APPLY_STATE_SYNC.set(false);
				break;
			case "APPLY_SYNC":
				TaskSwitch.APPLY_SYNC.set(false);
				break;
			case "PRODUCT_INFO_SYNC":
				TaskSwitch.PRODUCT_INFO_SYNC.set(false);
				break;
			case "ITEM_IMPORT":
				TaskSwitch.ITEM_IMPORT.set(false);
				break;
			case "ITEM_PUSH":
				TaskSwitch.ITEM_PUSH.set(false);
				break;
			case "ITEM_PREHANDL":
				TaskSwitch.ITEM_PREHANDL.set(false);
				break;
			case "TASK_CHECK":
				TaskSwitch.TASK_CHECK.set(false);
				break;
			default:
				return new Result(-1, 0, "Name: " + name + " 未知.");
		}

		return  Result.EMPTY;
	}


}
